#!spider/Scripts/python.exe
import logging
import multiprocessing
import os
import sys
import time
import win32gui

import win32con
from util.selenium import webdriver

from settings import base_path


class WindowOperate():

    @classmethod
    def file_upload(cls, filePath, browser_type='chrome'):
        '''
        windows 上传弹框已经可见，可以sleep1-2秒等待出现
        :param filePath: 文件绝对路径例 D:\\123.abc
        :param browser: 浏览器类型默认为 chrome
        :return:
        '''
        if browser_type.lower() == 'chrome':
            title = '打开'
        elif browser_type.lower() == 'Firefox':
            title = '文件上传'
        elif browser_type.lower() == 'ie':
            title = '选择要加载的文件'
        logging.info('本次使用{}进行上传操作'.format(browser_type))
        # 一级顶层窗口，此处title为上传窗口名称，浏览器不一样上传窗口名称不一样
        dialog = win32gui.FindWindow("#32770", title)
        # 二级窗口
        ComboBoxEx32 = win32gui.FindWindowEx(dialog, 0, "ComboBoxEx32", None)
        # 三级窗口
        comboBox = win32gui.FindWindowEx(ComboBoxEx32, 0, "ComboBox", None)
        # 四级窗口
        edit = win32gui.FindWindowEx(comboBox, 0, 'Edit', None)
        button = win32gui.FindWindowEx(dialog, 0, 'Button', None)
        # 执行操作 输入文件路径
        win32gui.SendMessage(edit, win32con.WM_SETTEXT, None, filePath)
        # 点击打开上传文件
        win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button)

def start():
    driver = webdriver.Chrome()
    driver.get('https://www.baidu.com')
    driver.maximize_window()  # 最大
    driver.find_element_by_class_name('soutu-btn').click()
    time.sleep(2)
    imgpath = base_path.replace('/', '\\') + "\\images\\code_py.jpg"
    # print(imgpath)
    driver.find_element_by_class_name('upload-pic').click()
    time.sleep(2)

    WindowOperate.file_upload(filePath=base_path.replace("/", "\\") + '\\images\\validate_code\\2020-03-09 14-37-31_validate_code_img.jpg')

    driver.quit()
if __name__ == '__main__':
    # restart_path = base_path.replace("/", "\\") + "\\test\\restart_test.py"
    pool = multiprocessing.Pool(processes=2)
    # s = sys.argv
    while True:

        pool.apply_async(start)
